package com.songshuai.pangu.api.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.songshuai.pangu.api.component.PhpSopComponent;
import com.songshuai.pangu.api.service.PhpSopService;
import com.songshuai.webapi.baseapi.constant.miniprogram.CacheKeys;
import com.songshuai.webapi.baseapi.dto.feign.response.phpsop.SopDto;
import com.songshuai.webapi.baseapi.dto.feign.response.phpsop.SopTypeDto;
import com.songshuai.webapi.baseapi.dto.pangu.request.phpsop.PhpSopReqDto;
import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.exception.BusinessException;
import com.songshuai.webapi.baseapi.pojo.QueryPageRes;
import com.songshuai.webapi.common.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author:wbl
 * @Data:2019/3/30
 */
@Service
public class PhpSopServiceImpl implements PhpSopService {

    @Autowired
    private PhpSopComponent phpSopComponent;
    @Autowired
    private RedisUtil redisUtil;

    @Override
    public List<SopTypeDto> getSopTypeListById(String typeId) {
        String result = phpSopComponent.getSopTypes(typeId);
        if (StringUtils.hasText(result)){
            JSONObject json = JSONObject.parseObject(result);
            if(1 == json.getInteger("success")){
                JSONArray jsonArray = json.getJSONArray("data");
                List<SopTypeDto> sopTypeList = new ArrayList<>();
                if(!CollectionUtils.isEmpty(jsonArray)){
                    jsonArray.forEach(val ->{
                        JSONObject data = (JSONObject) val;
                        SopTypeDto sopType = JSONObject.parseObject(data.toJSONString(), SopTypeDto.class);
                        sopTypeList.add(sopType);
                    });
                }
                return sopTypeList;
            }else{
                throw new BusinessException(json.getString("message"),ResultStatus.FAILOfficial);
            }
        }
        return null;
    }

    @Override
    public QueryPageRes<List<SopDto>> getSopList(PhpSopReqDto sopReqDto) {
        String result = phpSopComponent.getSopList(sopReqDto);
        if (StringUtils.hasText(result)){
            JSONObject json = JSONObject.parseObject(result);
            if(1 == json.getInteger("success")){
                QueryPageRes<List<SopDto>> queryPageRes = new QueryPageRes<>();
                JSONObject dataJson = json.getJSONObject("data");
                Long count = dataJson.getLong("pages");
                JSONArray dataList = dataJson.getJSONArray("list");
                List<SopDto> sopList = new ArrayList<>();
                queryPageRes.setCount(count);
                if(!CollectionUtils.isEmpty(dataList)){
                    dataList.forEach(val -> {
                        JSONObject sop = (JSONObject) val;
                        SopDto sopDto = JSONObject.parseObject(sop.toJSONString(), SopDto.class);
                        sopList.add(sopDto);
                    });
                }
                queryPageRes.setBody(sopList);
                return queryPageRes;
            }else{
                throw new BusinessException(json.getString("message"),ResultStatus.FAILOfficial);
            }
        }
        return null;
    }

    @Override
    public SopDto getSopDetail(String id) {
        String result = phpSopComponent.getSopDetail(id);
        if (StringUtils.hasText(result)){
            JSONObject json = JSONObject.parseObject(result);
            if(1 == json.getInteger("success")){
                JSONObject data = json.getJSONObject("data");
                SopDto sop = new SopDto();
                if(null != data){
                    sop = JSONObject.parseObject(data.toJSONString(), SopDto.class);
                }
                return sop;
            }else{
                throw new BusinessException(json.getString("message"),ResultStatus.FAILOfficial);
            }
        }
        return null;
    }
}
